$(function () {
    var pagingIndex
    var maxlistnum
    init()

    function init() {
        thislist()
        goodsList()


    }

    //使三级菜单的当前项随内容改变
    function thislist() {
        let thislist = getQueryString('category')
        $('.thislist').text(thislist)
    }
    var data
    //获取当前项的列表内容
    function goodsList() {
        if (pagingIndex == undefined) {
            pagingIndex = 1
        }
        let categoryid = parseInt(getQueryString('categoryid'))
        data = {
            'categoryid': categoryid,
            'pageid': pagingIndex
        }
        getList(data)
        goodsPaging()
    }

    //实现分页功能


    function getList(data) {
        console.log(data);
        $.ajax({
            url: 'http://193.112.55.79:9090/api/getproductlist',
            type: "get",
            data: data,
            success: (res) => {
                maxlistnum = res.totalCount
                let goodsdata = res.result
                console.log(res);
                let html = template('good_list', {
                    arr: goodsdata
                })
                $('.mm_goodslist').html(html)
            }

        })

    }

    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        // if (r != null) return unescape(r[2]);
        if (r != null) return decodeURI(r[2]);
        return null;
    }

    function goodsPaging() {
        pagingIndex = 1
        $('.up').on('tap', () => {
            if (pagingIndex == 1) {
                mui.toast('真的就没有拉')
                return
            }
            pagingIndex--
            console.log(pagingIndex);

            let categoryid = parseInt(getQueryString('categoryid'))
            data = {
                'categoryid': categoryid,
                'pageid': pagingIndex
            }
            getList(data)
        })
        $('.down').on('tap', () => {
            let goodsPagingMax = Math.ceil(maxlistnum / 10)
            if (pagingIndex >= goodsPagingMax) {
                mui.toast('这边也没有拉')
                return
            }
            pagingIndex++
            console.log(pagingIndex);
            let categoryid = parseInt(getQueryString('categoryid'))
            data = {
                'categoryid': categoryid,
                'pageid': pagingIndex
            }
            getList(data)
        })
    }
})